package logic;

import java.util.Collection;

import dataAccess.AgencyDB;
import dataType.AgencyData;
import exception.MyException;

public class Agency {

	private int idAgency;
	private String name;
	private String user;
	private String password;
	
	
	public Agency(){
		this.idAgency=0;
		this.name="";
		this.user="";
		this.password="";
	}
	
	public Agency(int idAgency, String name, String user, String password) {
		super();
		this.idAgency = idAgency;
		this.name = name;
		this.user = user;
		this.password = password;
	}
	public int getIdAgency() {
		return idAgency;
	}
	public void setIdAgency(int idAgency) {
		this.idAgency = idAgency;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getUser() {
		return user;
	}
	public void setUser(String user) {
		this.user = user;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public boolean autentication(int idAgency, String user, String password) throws MyException{
		AgencyDB adb = new AgencyDB();
		if (adb != null){
			Agency a = adb.find(idAgency);
			if (a != null){
				if (a.getUser().equals(user) && a.getPassword().equals(password))					
					return true;
			}			
		}
		return false;
	}
	
	public void save() throws MyException{
		AgencyDB adb = new AgencyDB();
		adb.save(this);
	}
	
	public boolean update() throws MyException{
		AgencyDB adb = new AgencyDB();
		adb.update(this);
		return true;
	}
	
	public Collection<AgencyData> getAgencies() throws MyException{
		AgencyDB adb = new AgencyDB();
		return adb.findall();
	}
}
